﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using HitachiLib.HitachiServiceRef;

namespace HitachiLib.Data
{
    public class Schedule
    {
        public const int HalfMonth = 1;
        public const int ThreeMonth = 2;
        public const int HalfYear = 3;
        public const int WholeYear = 4;

        public const int TimeMorning = 6;
        public const int TimeAfternoon = 12;
        public const int TimeEvening = 18;

        ScheduleData data = null;
        DateTime plan_date;
        Project project;

        public DateTime PlanDate
        {
            get { return plan_date; }
            set {
                plan_date = value;
                if (data == null)
                {
                    data = new ScheduleData();
                    data.ActionDate = Utils.DateUtils.GetDate(plan_date);
                    data.ActionTime = plan_date.Hour;
                    data.ID = 0;
                    data.StartTime = Utils.DateUtils.GetDate(DateTime.Now);
                }
            }
        }

        public ScheduleData Data
        {
            get { return data; }
            set {
                data = value;
                plan_date = Utils.DateUtils.GetDate(data.ActionDate);
                plan_date = plan_date.AddHours(data.ActionTime);
            }
        }

        public int ActionType
        {
            get { return Data.ActionType; }
            set { Data.ActionType = value; }
        }
        public Project Proj
        {
            get { return project; }
            set
            {
                project = value;
                if (project != null) Data.ProjectId = project.Id;
            }
        }
        public String PrjName
        {
            get { return Proj.Data.CustomName; }
        }
        public String PrjDevID
        {
            get { return Proj.Data.DevID; }
        }
        public String PrjContract
        {
            get { return Proj.Data.ContractType; }
        }
        public int HalfYearDiff
        {
            get
            {
                DateTime today =  DateTime.Today;
                DateTime target_date = Utils.DateUtils.GetDate(Proj.Data.ActionDate);
                target_date = target_date.AddMonths(6);
                DateTime next_date = Utils.DateUtils.GetNearest(target_date, Schedule.WholeYear);
                TimeSpan ts = next_date.Subtract(today);
                int d = (int)ts.TotalDays;
                return d;
            }
        }
        public String HalfYearDiffStr
        {
            get
            {
                int d = HalfYearDiff;
                if (d < -45 || d > 45)
                {
                    return "";
                }
                else
                    return "" + d;
            }
        }
        public int YearDiff
        {
            get
            {
                DateTime today = DateTime.Today;
                DateTime target_date = Utils.DateUtils.GetDate(Proj.Data.ActionDate);
                DateTime next_date = Utils.DateUtils.GetNearest(target_date, Schedule.WholeYear);
                TimeSpan ts = next_date.Subtract(today);
                int d = (int)ts.TotalDays;
                return d;
            }
        }
        public String YearDiffStr
        {
            get
            {
                int d = YearDiff;
                if (d < -45 || d > 45)
                {
                    return "";
                }
                else
                    return "" + d;
            }
        }
        public bool Quarter
        {
            get
            {
                DateTime today = DateTime.Today;
                DateTime target_date = Utils.DateUtils.GetDate(Proj.Data.ActionDate);
                DateTime next_date = Utils.DateUtils.GetNearest(target_date, Schedule.ThreeMonth);
                TimeSpan ts = next_date.Subtract(today);
                int d = (int)ts.TotalDays;

                if (d < 7 && d > -7)
                {
                    return true;
                }
                return false;
            }
        }
        public int AutoActionType
        {
            get
            {
                if (Data.ActionType == Schedule.HalfMonth)
                {
                    if (Quarter)
                    {
                        if (YearDiff < 45 && YearDiff > -45)
                            return Schedule.HalfMonth;
                        else if (YearDiff > 135 || YearDiff < -135)
                            return Schedule.HalfYear;
                        else
                            return Schedule.ThreeMonth;
                    }
                    else
                        return Schedule.HalfMonth;
                }
                else
                {
                    return Schedule.WholeYear;
                }

            }
        }
        public String AutoActionTypeStr
        {
            get
            {
                int t = AutoActionType;
                switch (t)
                {
                    case Schedule.WholeYear:
                        return "年度";
                    case Schedule.HalfYear:
                        return "年度";
                    case Schedule.ThreeMonth:
                        return "季度";
                    default:
                        return "常规";
                }
            }
        }
        public DayOfWeek DayWeek
        {
            get { return PlanDate.DayOfWeek; }
        }
        public String TimeDay
        {
            get
            {
                switch (PlanDate.Hour)
                {
                    case TimeMorning:
                        return "上午";
                    case TimeAfternoon:
                        return "下午";
                    default:
                        return "晚上";
                }
            }
        }

    }
}
